﻿using System;
using System.Text;

namespace RTools_NTS.Util
{
	[Serializable]
	public class StreamTokenizerSettings
	{
		public byte[] CharTypes
		{
			get
			{
				return this.charTypes;
			}
		}

		public bool GrabWhitespace
		{
			get
			{
				return this.grabWhitespace;
			}
			set
			{
				this.grabWhitespace = value;
			}
		}

		public bool GrabEol
		{
			get
			{
				return this.grabEol;
			}
			set
			{
				this.grabEol = value;
			}
		}

		public bool SlashSlashComments
		{
			get
			{
				return this.slashSlashComments;
			}
			set
			{
				this.slashSlashComments = value;
			}
		}

		public bool SlashStarComments
		{
			get
			{
				return this.slashStarComments;
			}
			set
			{
				this.slashStarComments = value;
			}
		}

		public bool GrabComments
		{
			get
			{
				return this.grabComments;
			}
			set
			{
				this.grabComments = value;
			}
		}

		public bool DoUntermCheck
		{
			get
			{
				return this.doUntermCheck;
			}
			set
			{
				this.doUntermCheck = value;
			}
		}

		public bool ParseNumbers
		{
            get
            {
                return this.parseNumbers;
            }
            set
            {
                if (value)
                {
                    for (int i = 48; i <= 57; i++)
                    {
                        byte[] array = this.charTypes;
                        int num = i;
                        array[num] |= 16;
                    }
                }
                else
                {
                    byte b = 16;
                    for (int j = 48; j <= 57; j++)
                    {
                        byte[] array2 = this.charTypes;
                        int num2 = j;
                        array2[num2] &= (byte)(~b);
                    }
                }
                this.parseNumbers = value;
            }
		}

		public bool ParseHexNumbers
		{
            get
            {
                return this.parseHexNumbers;
            }
            set
            {
                this.parseHexNumbers = value;
                if (this.parseHexNumbers)
                {
                    for (int i = 48; i <= 57; i++)
                    {
                        byte[] array = this.charTypes;
                        int num = i;
                        array[num] |= 32;
                    }
                    for (int j = 65; j <= 70; j++)
                    {
                        byte[] array2 = this.charTypes;
                        int num2 = j;
                        array2[num2] |= 32;
                    }
                    for (int k = 97; k <= 102; k++)
                    {
                        byte[] array3 = this.charTypes;
                        int num3 = k;
                        array3[num3] |= 32;
                    }
                    byte[] array4 = this.charTypes;
                    int num4 = 120;
                    array4[num4] |= 32;
                    return;
                }
                byte b = 32;
                for (int l = 65; l <= 70; l++)
                {
                    byte[] array5 = this.charTypes;
                    int num5 = l;
                    array5[num5] &= (byte)(~b);
                }
                for (int m = 97; m <= 102; m++)
                {
                    byte[] array6 = this.charTypes;
                    int num6 = m;
                    array6[num6] &= (byte)(~b);
                }
                byte[] array7 = this.charTypes;
                int num7 = 120;
                array7[num7] &= (byte)(~b);
            }
		}

		public StreamTokenizerSettings()
		{
			this.charTypes = new byte[StreamTokenizer.NChars + 1];
			this.SetDefaults();
		}

		public StreamTokenizerSettings(StreamTokenizerSettings other)
		{
			this.Copy(other);
		}

		public void Copy(StreamTokenizerSettings other)
		{
			this.charTypes = new byte[StreamTokenizer.NChars + 1];
			Array.Copy(other.charTypes, 0, this.charTypes, 0, this.charTypes.Length);
			this.grabWhitespace = other.grabWhitespace;
			this.grabEol = other.grabEol;
			this.slashSlashComments = other.slashSlashComments;
			this.slashStarComments = other.slashStarComments;
			this.grabComments = other.grabComments;
			this.doUntermCheck = other.doUntermCheck;
			this.parseHexNumbers = other.parseHexNumbers;
		}

		public bool SetDefaults()
		{
			this.slashStarComments = false;
			this.grabComments = false;
			this.slashSlashComments = false;
			this.grabWhitespace = false;
			this.doUntermCheck = true;
			this.grabEol = false;
			this.ResetCharTypeTable();
			this.ParseNumbers = true;
			this.ParseHexNumbers = true;
			this.WordChars(65, 90);
			this.WordChars(97, 122);
			this.WhitespaceChars(0, 32);
			this.QuoteChar(39);
			this.QuoteChar(34);
			this.WordChars(48, 57);
			return true;
		}

		public bool SetupForCodeParse()
		{
			this.GrabWhitespace = true;
			this.GrabComments = true;
			this.SlashSlashComments = true;
			this.DoUntermCheck = true;
			this.SlashStarComments = true;
			this.WordChar(95);
			this.ParseNumbers = true;
			this.ParseHexNumbers = true;
			return true;
		}

		public void ResetCharTypeTable()
		{
			Array.Clear(this.charTypes, 0, this.charTypes.Length);
			this.charTypes[StreamTokenizer.NChars] = 64;
		}

		public void WordChar(int c)
		{
			byte[] array = this.charTypes;
			array[c] |= 1;
		}

		public void WordChars(int startChar, int endChar)
		{
			for (int i = startChar; i <= endChar; i++)
			{
				byte[] array = this.charTypes;
				int num = i;
				array[num] |= 1;
			}
		}

		public void WordChars(string s)
		{
            for (int i = 0; i < s.Length; i++)
            {
                byte[] array = this.charTypes;
                char c = s[i];
                array[(int)c] = (byte)((array[(int)c] | 1));
            }
		}

		public void WhitespaceChar(int c)
		{
			this.charTypes[c] = 4;
		}

		public void WhitespaceChars(int startChar, int endChar)
		{
			for (int i = startChar; i <= endChar; i++)
			{
				this.charTypes[i] = 4;
			}
		}

		public void OrdinaryChars(int startChar, int endChar)
		{
			for (int i = startChar; i <= endChar; i++)
			{
				this.charTypes[i] = 0;
			}
		}

		public void OrdinaryChar(int c)
		{
			this.charTypes[c] = 0;
		}

		public void CommentChar(int c)
		{
			this.charTypes[c] = 2;
		}

		public void QuoteChar(int c)
		{
			this.charTypes[c] = 8;
		}

		public string CharTypeToString(byte ctype)
		{
			StringBuilder stringBuilder = new StringBuilder();
			if (this.IsCharType(ctype, CharTypeBits.Quote))
			{
				stringBuilder.Append('q');
			}
			if (this.IsCharType(ctype, CharTypeBits.Comment))
			{
				stringBuilder.Append('m');
			}
			if (this.IsCharType(ctype, CharTypeBits.Whitespace))
			{
				stringBuilder.Append('w');
			}
			if (this.IsCharType(ctype, CharTypeBits.Digit))
			{
				stringBuilder.Append('d');
			}
			if (this.IsCharType(ctype, CharTypeBits.Word))
			{
				stringBuilder.Append('a');
			}
			if (this.IsCharType(ctype, CharTypeBits.Eof))
			{
				stringBuilder.Append('e');
			}
			if (stringBuilder.Length == 0)
			{
				stringBuilder.Append('c');
			}
			return stringBuilder.ToString();
		}

		public bool IsCharType(byte ctype, CharTypeBits type)
		{
			return (ctype & (byte)type) > 0;
		}

		public bool IsCharType(char c, CharTypeBits type)
		{
			return (this.charTypes[(int)c] & (byte)type) > 0;
		}

		public bool IsCharType(int c, CharTypeBits type)
		{
			return (this.charTypes[c] & (byte)type) > 0;
		}

		public void Display()
		{
			this.Display(string.Empty);
		}

		public void Display(string prefix)
		{
		}

		private byte[] charTypes;

		private bool grabWhitespace;

		private bool grabEol;

		private bool slashSlashComments;

		private bool slashStarComments;

		private bool grabComments;

		private bool doUntermCheck;

		private bool parseNumbers;

		private bool parseHexNumbers;
	}
}
